package com.egg.mall.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.common.core.model.controller.ApiController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.mall.entity.spu.Spu;
import com.egg.mall.entity.tag.TagSpuLinkSpu;
import com.egg.mall.model.response.api.TagSpuLinkSpuPage;
import com.egg.mall.service.spu.ISpuService;
import com.egg.mall.service.tag.ITagSpuLinkSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author east
 * @since 2023-05-09
 */
@Slf4j
@Api(tags = ApiController.API_PREFIX + "分组SPU关联SPU")
@RestController
@RequestMapping("/tag/spu/link/spu")
public class ApiTagSpuLinkSpuController extends ApiController {


    @Autowired
    private ITagSpuLinkSpuService tagSpuLinkSpuService;
    @Autowired
    private ISpuService spuService;


    @ApiOperation(value = "分组SPU关联SPU查询")
    @GetMapping("/list/{tagSpuId}")
    public ResponseData<PageData<TagSpuLinkSpuPage>> noticeListGet(
            @PathVariable String tagSpuId
            , @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        LambdaQueryWrapper<TagSpuLinkSpu> qw = tagSpuLinkSpuService.buildLambdaSortQw();
        qw.eq(TagSpuLinkSpu::getTagSpuId, tagSpuId);
        Page<TagSpuLinkSpu> dbPage = tagSpuLinkSpuService.page(new Page<>(current, pageSize), qw);
        List<Spu> spuList = spuService.baseFindById(dbPage.getRecords().stream().map(o -> o.getSpuId()).collect(Collectors.toSet()));
        Map<Long, Spu> spuMap = spuList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o));
        List<TagSpuLinkSpuPage> pageList = new ArrayList<>();
        dbPage.getRecords().forEach(o -> {
            TagSpuLinkSpuPage temp = new TagSpuLinkSpuPage();
            Spu spu = spuMap.getOrDefault(o.getSpuId(), new Spu());
            temp.setSpuId(spu.getId());
            temp.setSpuName(spu.getName());
            temp.setPicUrl(spu.getPicUrl());
            temp.setPrice(spu.getPrice());
            temp.setDescription(spu.getDescription());
            pageList.add(temp);
        });
        return ResponseData.success(new PageData<>(dbPage, pageList));
    }


}

